package com.example.springredis.demos;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @RequestMapping("/testString")
    @ResponseBody
    public String testString() {
        redisTemplate.opsForValue().set("key","value");
        redisTemplate.opsForValue().set("key2","value2");
        String s = redisTemplate.opsForValue().get("key");
        System.out.println(s);
        return "ok:"+s;
    }


    @RequestMapping("/testList")
    @ResponseBody
    public String testList() {
        redisTemplate.opsForList().leftPush("list","value");
        redisTemplate.opsForList().leftPush("list","value2");
        String s = redisTemplate.opsForList().leftPop("list");
        System.out.println(s);
        return "ok:"+s;
    }

    @RequestMapping("/testHash")
    @ResponseBody
    public String testHash() {
        redisTemplate.opsForHash().put("hash","key","value");
        redisTemplate.opsForHash().put("hash","key2","value2");
        String s = (String) redisTemplate.opsForHash().get("hash","key");
        System.out.println(s);
        return "ok:"+s;
    }

    @RequestMapping("/testSet")
    @ResponseBody
    public String testSet() {
        redisTemplate.opsForSet().add("set","value");
        redisTemplate.opsForSet().add("set","value2");
        String s = (String) redisTemplate.opsForSet().pop("set");
        System.out.println(s);
        return "ok:"+s;
    }

    @RequestMapping("/testZSet")
    @ResponseBody
    public String testZSet() {
        redisTemplate.opsForZSet().add("zset","value",1);
        redisTemplate.opsForZSet().add("zset","value2",2);
        redisTemplate.opsForZSet().count("zset",1,2);
        redisTemplate.opsForZSet().rank("zset","value");

        return "ok:"+redisTemplate.opsForZSet().count("zset",1,2);
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test() {

       redisTemplate.execute((RedisConnection connection) -> {
          connection.flushAll();
          return null;
       });
return "通过execute方法调用redis的api，也就是Jedis";
    }
}
